
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否上线" prop="status">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.status" placeholder="请选择" style="width: 256px">
            <el-option :value="1" label="上线"></el-option>
            <el-option :value="0" label="下线"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="handleEdit('新增', {})">
          <template #icon><Plus /></template>
          新增
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="城市名称" min-width="120" prop="cityName" align="center"></el-table-column>
        <el-table-column label="备注" min-width="120" prop="remark" align="center"></el-table-column>
        <el-table-column label="状态" min-width="120" prop="status" align="center">
          <template #default="scope">
            <div v-if="scope.row.status == 1" style="color: green">上线</div>
            <div v-else style="color: red">下线</div>
          </template>
        </el-table-column>
        <el-table-column label="渠道" min-width="60" prop="channelId" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.channelId" :key="index">
              {{ channelEnum[item] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="平台" min-width="80" prop="platformId" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.platformId" :key="index">
              {{ platFormEnum[item] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="业务类型" min-width="80" prop="orderType" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.orderType" :key="index">
              {{ orderTypeEnum[item] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="车型" min-width="120" prop="vehicleTypeId" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.vehicleTypeId" :key="index">
              {{ carTypeEnum[item] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="区域加价" min-width="190" prop="areaPrice" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.areaPrice" :key="index">{{ item.province }}-{{ item.cityName }}-{{ item.district }}-{{ item.percent }}%-{{ item.price }}元</div>
          </template>
        </el-table-column>
        <el-table-column label="时段加价" min-width="160" prop="periodPrice" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.periodPrice" :key="index">{{ item.start }}-{{ item.end }}-{{ item.percent }}%-{{ item.price }}</div>
          </template>
        </el-table-column>
        <el-table-column label="急单加价" min-width="80" prop="urgentPrice" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.urgentPrice" :key="index">{{ item.time }}-{{ item.price }}</div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div style="display: flex; justify-content: center; align-items: center">
              <el-button type="primary" link size="small" @click.stop="handleEdit('编辑', scope.row)">
                <template #icon><Pointer /></template>
                编辑
              </el-button>
              <el-button type="primary" link size="small" @click.stop="handleDel(scope.row)">
                <template #icon><Pointer /></template>
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <FormModal ref="formModal" @success="handleSuccess" />
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from "vue";
import CommonApi from "@/api/common";
import FormModal from "./comoponents/modal.vue";
import {
  orderStatusEnum,
  platFormEnum,
  channelEnum,
  orderTypeEnum,
  bizTypeEnum,
  luggageEnum,
  channelArr,
  orderStatusArr,
  bizTypeArr,
  orderTypeArr,
  platFormArr,
  carTypeArr,
  carTypeEnum,
} from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import JntApi from "@/api/jnt";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const formModal: any = ref(null);

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

function handleDel(row: any) {
  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      JntApi.BatchDel({ id: row.id })
        .then(() => {
          ElMessage.success("删除成功");
          handleQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}

function handleSuccess() {
  handleQuery();
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  JntApi.getProductBatchList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

//add or edit callBack handle
function handleEdit(title: String, row: Object) {
  //title enum : 新增 / 编辑  row enum: {...formData} or {}
  nextTick(() => {
    formModal.value.edit(title, row);
  });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
